package com.example.service.impl;

import com.example.mapper.*;
import com.example.model.*;
import com.example.model.Class;
import com.example.service.ClassTableRelationService;
import com.example.util.DataMap;
import com.example.util.ErrorStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: tao95
 * Date: 2017/5/11
 * Time: 21:05
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class ClassTableRelationServiceImpl implements ClassTableRelationService {

    @Autowired
    private ClassTableRelationMapper ctrMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private LaboratoryMapper labMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private CourseMapper courseMapper;

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public ModelAndView findTimeTable(ClassTableRelation classTableRelation, ModelAndView model) {
        List<ClassTableRelationVO> ctrList = ctrMapper.findTimeTable(classTableRelation);
        model.addObject("days","星期"+classTableRelation.getDaysId());
        boolean []flag = new boolean[4];
        for (ClassTableRelationVO ctrv: ctrList) {
            Teacher teacher = teacherMapper.findTeacherById(ctrv.getTeacherId());
            ctrv.setTeacherPhone(teacher.getPhone());
            if(ctrv.getCourseTime() == 1){
                flag[0] = true;
                model.addObject("oneCourse",ctrv);
            }
            if(ctrv.getCourseTime() == 2){
                flag[1] = true;
                model.addObject("secondCourse",ctrv);
            }
            if(ctrv.getCourseTime() == 3){
                flag[2] = true;
                model.addObject("threeCourse",ctrv);
            }
            if(ctrv.getCourseTime() == 4){
                flag[3] = true;
                model.addObject("fourCourse",ctrv);
            }
        }
        return model;
    }

    @Override
    public ModelAndView getAllData(ModelAndView model) {
        List<Laboratory> labList = labMapper.selectLabList();
        List<Class> classList = classMapper.selectClassList();
        List<Teacher> teacherList = teacherMapper.selectTeacherList();
        List<Course> courseList = courseMapper.selectCourseList();
        model.addObject("labList",labList);
        model.addObject("classList",classList);
        model.addObject("teacherList",teacherList);
        model.addObject("courseList",courseList);
        return model;
    }

    @Override
    public DataMap addCTR(ClassTableRelationVO classTableRelation) {

        Laboratory laboratory = labMapper.selectById(classTableRelation.getLaboratoryId());
        Teacher teacher = teacherMapper.selectById(classTableRelation.getTeacherId());
        Class aClass = classMapper.selectById(classTableRelation.getClassId());
        Course course = courseMapper.selectById(classTableRelation.getCourseId());

        classTableRelation.setClassName(aClass.getClassName());
        classTableRelation.setCourseName(course.getCourseName());
        classTableRelation.setLaboratoryName(laboratory.getName());
        classTableRelation.setTeacherName(teacher.getTeacherName());
        classTableRelation.setTeacherPhone(teacher.getPhone());
        DataMap dataMap = new DataMap();
        if(classTableRelation.getId() == null){
            try {
                ctrMapper.insertCTR(classTableRelation);
            }catch(Exception e){
                logger.error(e.getMessage());
                dataMap.put("code", ErrorStatusEnum.ADD_CTRFAIL.getErrorCode());
                return dataMap;
            }
        }else{
            try{
                ctrMapper.editCTR(classTableRelation);
            }catch (Exception e){
                logger.error(e.getMessage());
                dataMap.put("code", ErrorStatusEnum.EDIT_CTRFAIL.getErrorCode());
                return dataMap;
            }
        }
        dataMap.put("code", ErrorStatusEnum.SUCCESS.getErrorCode());
        return dataMap;
    }

    @Override
    public ClassTableRelation selectById(Integer id) {
        ClassTableRelation ctr = ctrMapper.selectById(id);
        return ctr;
    }

    @Override
    public DataMap delCTR(Integer id) {
        DataMap dataMap = new DataMap();
        try{
            ctrMapper.delCTR(id);
        }catch (Exception e){
            logger.error(e.getMessage());
            dataMap.put("code", ErrorStatusEnum.DEL_CTRFAIL.getErrorCode());
            return dataMap;
        }
        dataMap.put("code", ErrorStatusEnum.SUCCESS.getErrorCode());
        return dataMap;
    }
}